package com.kvest.view_pager_test.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.kvest.view_pager_test.FragmentDialog.DialogFromLayout;
import com.kvest.view_pager_test.R;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 17.03.13
 * Time: 18:39
 * To change this template use File | Settings | File Templates.
 */
public class CustomListFragment extends ListFragment {
    private static final String LAYOUT_ID_EXTRA = "layout_id";
    private static final long DELAY = 4000;
    private ArrayAdapter<String> adapter;
    private int layoutId;
    private boolean restoging;

    public CustomListFragment() {
        this(android.R.layout.simple_list_item_1);
    }

    public CustomListFragment(int layoutId) {
        super();
        this.layoutId = layoutId;
        this.restoging = false;
    }

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        restoging = (savedState != null);
        if (savedState != null && savedState.containsKey(LAYOUT_ID_EXTRA)) {
            layoutId = savedState.getInt(LAYOUT_ID_EXTRA);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_list_fragment, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new ArrayAdapter<String>(getActivity(), layoutId, new ArrayList<String>());
        setListAdapter(adapter);

        if (restoging) {
            for (int i = 0; i < 10; ++i) {
                adapter.add("String " + i);
            }
            adapter.notifyDataSetChanged();
        } else {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; ++i) {
                        adapter.add("String " + i);
                    }
                    adapter.notifyDataSetChanged();
                }
            }, DELAY);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        DialogFromLayout dialog = new DialogFromLayout(adapter.getItem(position));
        dialog.show(getFragmentManager(), null);
    }

    @Override
    public void onSaveInstanceState(Bundle savedObject) {
        Log.d("SAVE", "onSaveInstanceState");
        super.onSaveInstanceState(savedObject);
        savedObject.putInt(LAYOUT_ID_EXTRA, layoutId);
    }
}
